package cn.sciento.config.client.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import cn.sciento.config.client.api.impl.CompositeConfigClient;
import cn.sciento.config.client.api.impl.DefaultConfigClient;
import cn.sciento.config.client.api.impl.NacosConfigClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class ConfigClientAutoConfiguration {
    @Primary
    @Bean
    public ConfigClient compositeConfigClient(Optional<List<ConfigClient>> optional) {
        return new CompositeConfigClient(optional.orElse(new ArrayList<>()));
    }

    @Bean
    @ConditionalOnClass(name = {"org.springframework.cloud.config.client.ConfigServicePropertySourceLocator"})
    public ConfigClient defaultConfigClient() {
        return new DefaultConfigClient();
    }

    @Bean
    @ConditionalOnClass(name = {"com.alibaba.cloud.nacos.client.NacosPropertySourceLocator"})
    public ConfigClient nacosConfigClient() {
        return new NacosConfigClient();
    }
}
